<?php

//加载需要的文件
require_once "top/TopClient.php";
require_once "top/TopLogger.php";
require_once "top/RequestCheckUtil.php";
require_once "top/ResultSet.php";
require_once "top/request/AlibabaAliqinFcSmsNumSendRequest.php";

class AliMsgSend{
	//定义key
	private $appkey = null;
	private $secretKey = null;

	public function __construct($_appkey, $_secretKey)
	{
		$this->appkey = $_appkey;
		$this->secretKey = $_secretKey;
	}

	public function __call ($name, $arguments)
	{
		return "The function is not exist!";
	}


	/**阿里大鱼验证码发送接口
	 * @param $_RecNum string 手机号,
	 * @param $_smsParam json 短信模板变量,
	 * @param $_smsTemplateCode string 短信模板id,
	 * @param $_extend string 公共回传参数,
	 * @param $_smsFreeSignName string 短信签名,
	 * @param $_smsType string 短信类型 defult normal,
	 * @return bool
	 */
	public function sendMsg($_RecNum, $_smsParam, $_smsTemplateCode, $_smsFreeSignName, $_extend = "", $_smsType = "normal")
	{
		$c = new TopClient;
		$c ->appkey = $this->appkey;
		$c ->secretKey = $this->secretKey;
		$req = new AlibabaAliqinFcSmsNumSendRequest;
		$req ->setExtend($_extend);
		$req ->setSmsType($_smsType);
		$req ->setSmsFreeSignName($_smsFreeSignName);
		$req ->setSmsParam($_smsParam);
		$req ->setRecNum($_RecNum);
		$req ->setSmsTemplateCode($_smsTemplateCode);
		$resp = $c ->execute($req);
		return $this->sendMsgResult($resp);
	}

	/**
	 * 对接口返回值进行处理
	 * @param null $_resp
	 * @return bool 这里只判断0和1两种状态，1 表示成功，0 可能是账户未开通/短信超过数量/加入黑名单 等等
	 */
	public function sendMsgResult($_resp = null)
	{
		if ($_resp->result->success && !$_resp->result->code) {
			return true;
		} else if ($_resp->code || $_resp->msg == "Remote service error") {
			return false;
		} else {
			return false;
		}
	}
}